package org.aceor.tennis.web.dto;

import org.aceor.tennis.common.enums.ErrorCode;

import java.io.Serializable;

public class BaseResult<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -2161182621547021545L;


    private boolean success;
    private String errCode;
    private String errMsg;
    private T result;

    public BaseResult() {
    }

    public BaseResult(boolean success, T result) {
        this.success = success;
        this.result = result;
    }

    public BaseResult(boolean success, String message, ErrorCode errorCode) {
        this(success, message, errorCode.getErrorCode());
    }

    public BaseResult(boolean success, ErrorCode errorCode) {
        this(success, errorCode.getErrorMessage(), errorCode.getErrorCode());
    }

    public BaseResult(boolean success, String message) {
        this(success, message, "");
    }

    public BaseResult(boolean success, String message, String errorCode) {
        this.success = success;
        this.errMsg = message;
        this.errCode = errorCode;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public void setErrorCode(ErrorCode errCode) {
        this.errCode = errCode.getErrorCode();
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    @Override
    public String toString() {
        return String.format("success :%s, errCode :%s, errMsg :%s, result :%s", success, errCode, errMsg, result);
    }

    public static BaseResult buildError(ErrorCode errCode, String message) {
        return new BaseResult(false, message, errCode);
    }

    public static BaseResult buildError(ErrorCode errCode) {
        return new BaseResult(false, errCode);
    }

    public static BaseResult buildError(String errCode, String message) {
        return new BaseResult(false, message, errCode);
    }

    public static BaseResult buildError(String message) {
        return new BaseResult(false, message);
    }

    public static <T> BaseResult<T> buildSuccess(T content) {
        return new BaseResult<T>(true, content);
    }
}
